﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Antix.Data.Objects
{
    /// <summary>
    /// <para>Field attribute</para>
    /// 
    /// <para>Defines the field held in the database for a field on a data enabled object</para>
    /// 
    /// <list type="table">
    /// <item><term>Name</term><description>overriding name for the field or an sql subquery</description></item>
    /// <item><term>Load</term><description>load the field from the databasev</description></item>
    /// <item><term>Save</term><description>save the field to the database</description></item>
    /// <item><term>Create</term><description>create the field in the database</description></item>
    /// <item><term>Length</term><description>length of a field, default value is 50</description></item>
    /// <item><term>Default</term><description>default to set in the database can be used for sql functions such as getdate()</description></item>
    /// </list>
    /// </summary>
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public class DataColumnAttribute : Attribute
    {
        #region Constructors etc.

        /// <summary>
        /// Create a new Field attribute.
        /// 
        /// Optional values for Name, Load, Save, Create, Length and Default
        /// </summary>
        public DataColumnAttribute()
        {
            this._Name = null;
            this._Load = true;
            this._Save = true;
            this._Create = true;
            this._Length = 50;
            this._VariableLength = true;
            this._Default = null;
            this._AllowNull = false;

            //_CascadeSave = false;
            //_CascadeDelete = false;

            this._ReferencedType = null;
            this._DeleteAction = DeleteActionEnum.None;
        }

        #endregion

        #region Name

        private string _Name;

        /// <summary>
        /// <para>Override for the name of the field in the database</para>
        /// </summary>
        public string Name
        {
            get { return this._Name; }
            set
            {
                this._Name = value;
                if (this._Name != null
                    && this._Name.StartsWith("("))
                {
                    this._Save = false;
                    this._Create = false;
                }
            }
        }

        #endregion

        #region NameOriginal

        private string _NameOriginal;

        /// <summary>
        /// <para>Original name of the field in the database, if changed</para>
        /// </summary>
        public string NameOriginal
        {
            get { return _NameOriginal; }
            set { _NameOriginal = value; }
        }

        #endregion

        #region Load / Save / Create

        private bool _Load;
        private bool _Save;
        private bool _Create;

        /// <summary>
        /// The data in this field will be loaded from the database
        /// </summary>
        public bool Load
        {
            get { return _Load; }
            set { _Load = value; }
        }

        /// <summary>
        /// The data in this field will be saved to the database
        /// </summary>
        public bool Save
        {
            get { return _Save; }
            set { _Save = value; }
        }

        /// <summary>
        /// This field will be created in the database
        /// </summary>
        public bool Create
        {
            get { return _Create; }
            set { _Create = value; }
        }

        #endregion

        #region Length

        private int _Length;

        /// <summary>
        /// Length of the field in the database
        /// </summary>
        public int Length
        {
            get { return _Length; }
            set { _Length = value; }
        }

        #endregion

        #region VariableLength

        private bool _VariableLength;

        /// <summary>
        /// VariableLength of the field in the database
        /// </summary>
        public bool VariableLength
        {
            get { return _VariableLength; }
            set { _VariableLength = value; }
        }

        #endregion

        #region Default

        private string _Default;

        /// <summary>
        /// The default value for the field in the database
        /// </summary>
        public string Default
        {
            get { return _Default; }
            set { _Default = value; }
        }

        #endregion

        #region AllowNull

        private bool _AllowNull;

        /// <summary>
        /// This field will be created in the database
        /// </summary>
        public bool AllowNull
        {
            get { return _AllowNull; }
            set { _AllowNull = value; }
        }

        #endregion

        #region ReferencedType

        private Type _ReferencedType;

        /// <summary>
        /// <para>Referenced object type</para>
        /// </summary>
        public Type ReferencedType
        {
            get { return _ReferencedType; }
            set { _ReferencedType = value; }
        }

        #endregion

        #region DeleteAction

        private DeleteActionEnum _DeleteAction;

        /// <summary>
        /// <para>Action on delete of a linked object</para>
        /// </summary>
        public DeleteActionEnum DeleteAction
        {
            get { return _DeleteAction; }
            set { _DeleteAction = value; }
        }

        #endregion

        #region FieldInfo

        public FieldInfo FieldInfo { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// <para>String representation</para>
        /// </summary>
        public override string ToString()
        {
            return Name + " (" + Length + ")";
        }

        #endregion
    }
}
